package com.shengdou.as.slopsapptest.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shengdou.as.slopsapptest.MyApplication;
import com.shengdou.as.slopsapptest.R;

public class ToastUtils {

    private static Toast toast;

    public ToastUtils() {

    }

/*	*//**
     * 弹出Toast提示,其他的几个构造方法类似用法
     *
     * @param stringRes
     *            设置提示的字符
     * @param duration
     *            间隔时间
     *//*
    public static void showToast(int stringRes, int duration) {
		Toast.makeText(MyApplication.app, stringRes + "", duration).show();
	}

	public static void showToast(String stringRes, int duration) {
		Toast.makeText(MyApplication.app, stringRes + "", duration).show();
	}*/


    /**
     * 多次点击只显示一次解决方案
     * 错误提示
     *
     * @param s
     */
    public static void showToast(String s) {
        Context context = MyApplication.ApplicationContext;

        View view;
        TextView tv;
        ImageView img = null;
        if (toast == null) {
            toast = new Toast(context);
            toast.setGravity(Gravity.TOP, 0, 200);
            toast.setDuration(Toast.LENGTH_SHORT);
            view = LinearLayout.inflate(context, R.layout.toastview, null);
            tv = (TextView) view.findViewById(R.id.toastview_tv);
//			img = (ImageView) view.findViewById(R.id.toastview_img);
            tv.setText(s);
//			img.setImageResource(R.drawable.tishi_ico_msg_normal);
        } else {
            view = LinearLayout.inflate(context, R.layout.toastview, null);
            tv = (TextView) view.findViewById(R.id.toastview_tv);
//			img = (ImageView) view.findViewById(R.id.toastview_img);
            tv.setText(s);
//			img.setImageResource(R.drawable.tishi_ico_msg_normal);
            toast.setDuration(Toast.LENGTH_SHORT);
        }

        if (view != null) {
            toast.setView(view);
        }
        if (toast != null) {

            toast.show();
        }

    }

    public static void showToast(int resId) {
        Context context = MyApplication.ApplicationContext;
        showToast(context.getString(resId));
    }


    public static void showLongToast(String s) {
        Context context = MyApplication.ApplicationContext;
        View view = null;
        TextView tv = null;
        ImageView img = null;
        if (toast == null) {
            toast = new Toast(context);
            toast.setGravity(Gravity.TOP, 0, 200);
            toast.setDuration(Toast.LENGTH_LONG);
            view = LinearLayout.inflate(context, R.layout.toastview, null);
            tv = (TextView) view.findViewById(R.id.toastview_tv);
//			img = (ImageView) view.findViewById(R.id.toastview_img);
            tv.setText(s);
//			img.setImageResource(R.drawable.tishi_ico_msg_normal);
        } else {
            view = LinearLayout.inflate(context, R.layout.toastview, null);
            tv = (TextView) view.findViewById(R.id.toastview_tv);
//			img = (ImageView) view.findViewById(R.id.toastview_img);
            tv.setText(s);
//			img.setImageResource(R.drawable.tishi_ico_msg_normal);
            toast.setDuration(Toast.LENGTH_LONG);
        }
        if (view != null) {
            toast.setView(view);
        }
        if (toast != null) {
            toast.show();
        }

    }


    /**
     * 多次点击只显示一次解决方案
     * 正确提示
     *
     * @param s
     */
    public static void showToast2(String s) {
        Context context = MyApplication.ApplicationContext;
        View view = null;
        TextView tv = null;
        ImageView img = null;
        if (toast == null) {
            toast = new Toast(context);
            toast.setGravity(Gravity.TOP, 0, 200);
            toast.setDuration(Toast.LENGTH_SHORT);
            view = LinearLayout.inflate(context, R.layout.toastview, null);
            tv = (TextView) view.findViewById(R.id.toastview_tv);
//			img = (ImageView) view.findViewById(R.id.toastview_img);
            tv.setText(s);
//			img.setImageResource(R.drawable.tishi_zhengque_ico_msg_normal);
        } else {
            view = LinearLayout.inflate(context, R.layout.toastview, null);
            tv = (TextView) view.findViewById(R.id.toastview_tv);
//			img = (ImageView) view.findViewById(R.id.toastview_img);
            tv.setText(s);
//			img.setImageResource(R.drawable.tishi_zhengque_ico_msg_normal);
            toast.setDuration(Toast.LENGTH_SHORT);
        }

        if (view != null) {
            toast.setView(view);

        }
        if (toast != null) {
            toast.show();
        }
    }

    public static void showToast2(int resId) {
        Context context = MyApplication.ApplicationContext;
        showToast2(context.getString(resId));
    }

    public static void cancel() {
        if (toast != null) {
            toast.cancel();
        }
    }


}
